﻿Type=Service
Version=5.8
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
#Region  Service Attributes 
	#StartAtBoot: False
	#ExcludeFromLibrary: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Dim dispopt,dispopt_nocache As uilOptions
	Private aLoc As AMapLocationClient
	Private aloc_option As locationOption
	Public LocCity As AMapLocation
	'初始化数据的flg
	Public ui As myInfo
	Public nNetReq As Int
	Public nUnrateOrd As Int=0
	Private bugly As bugly
	Public getui As b4agetui
	Public jmsg As JMessage
End Sub

Sub Service_Create
	'This is the program entry point.
	bugly.startCatching(comm.BUGLY_APPID)	
	getui.Init("gtpush")
	getui.OnStart
	jmsg.init("jmsg",True)
	Log(getui.isPushTurnedOn)
	'This is a good place to load resources that are not specific to a single activity.
	dispopt.Initialize
	g_store.conf.Initialize(File.DirInternal,"kvstore.dat")
	'dispopt.resetViewBeforeLoading(True)
	dispopt.cacheInMemory(False).cacheOnDisc(True)
	dispopt_nocache.Initialize
	dispopt_nocache.cacheInMemory(False).cacheOnDisc(False).resetViewBeforeLoading(True)
	comm.uilnocache.Initialize("uilnocache",dispopt_nocache.build)
	comm.uil.Initialize("uil",dispopt.build)
	inittestdata
	
End Sub
Sub jmsg_gotResult(responseCode As Int,responseMessage As String,JsonStr As String)
	comm.L("jmsg:"&responseCode&" "&responseMessage&" "&JsonStr)
End Sub
Sub gtpush_onRec(str As String,messageid As String,taskid As String)
	Log("gotPush:"&str)
End Sub
Sub gtpush_onClickid(str As String)
	Log("pushclicked:"&str)
End Sub
Sub Service_Start (StartingIntent As Intent)
	nNetReq=3
	If g_store.getDef("islogin",False) Then
		nNetReq=nNetReq+1
	End If
	Dim cs As String=g_store.getDef("cityjs","")
	If cs.Length<1 Then
		nNetReq=nNetReq+1
	End If
	initloc '后台定位一次
	initdata '初始化网络数据
	'doLogin("123456789","fdsafasd")
	If g_store.conf.GetDefault("islogin",False) Then
		ui=g_store.conf.Get("myinfo")
	End If
End Sub
#Region init
Sub initdata
	ParseHelper.doGetReq(Me,"banner",comm.getApi(comm.apiIdx.APPBANNER),Null)
	ParseHelper.doGetReq(Me,"kemu",comm.getApi(comm.apiIdx.KEMU),Null)
	ParseHelper.doGetReq(Me,"mstj",comm.getApi(comm.apiIdx.MSTJ),Null)
	If g_store.getDef("islogin",False) Then
		ParseHelper.doGetReq(Me,"unrate",comm.getApi(comm.apiIdx.REQUNRATENUM),Null)
	End If
	ParseHelper.doGetReq(Me,"getcitys",comm.getApi(comm.apiIdx.REQCITYLIST),Null)
End Sub
Sub inittestdata
'	Dim arr() As String=Array As String("语文","数学","英语","物理","化学","地理","历史","奥数","生物","政治")
	Dim lst As List
'	lst.Initialize
'	For i=0 To arr.Length-1
'		Dim st As Subject
'		st.Initialize
'		st.name=arr(i)
'		st.img="http://assets.alicdn.com/apps/mytaobao/3.0/profile/defaultAvatar/avatar-60.png"
'		lst.Add(st)
'	Next
'	Dim lst As List
'	lst.Initialize
'	g_store.conf.Put(comm.S_SUBJECTS,lst)
	lst.Initialize
	lst.AddAll(Array As String("智能排序","学生数最多","评论最多","评论最低","金额最高","金额最低"))
	g_store.conf.Put(comm.S_PAIXU_FINDTEACHER,lst)
	lst.Initialize()
	lst.AddAll(Array As String("一年级","二年级","三年级","四年级","五年级","六年级","初一","初二","初三","高一","高二","高三"))
	g_store.conf.Put(comm.S_NIANJI_FINDTEACHER,lst)
	lst.Initialize
	lst.AddAll(Array As String("乌鲁木齐市","克拉玛依市","吐鲁番市","哈密市","昌吉市","阜康市","米泉市","博乐市","库尔勒市","阿克苏市","阿图什市","喀什市","和田市","奎屯市","伊宁市","塔城市","乌苏市","阿勒泰市"))
	g_store.conf.Put(comm.S_CITYLIST,lst)
	lst.Initialize
	lst.AddAll(Array As String("学生上门","老师上门","协商地址"))
	g_store.conf.Put(comm.S_SKFS,lst)
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Log("UnhandleErr:"&Error&CRLF&StackTrace)
	Msgbox("不好意思，程序出错咯","错误")
	Return True
End Sub
#End Region
#Region 网络请求


Sub JobDone(thj As HttpJob)
	If thj.Success Then
		Dim str As String=thj.GetString
		Log("Job:"&thj.JobName&" js:"&str)
		Dim nr As netRes=ParseHelper.getStatusData(str)
		Select thj.JobName
		Case "banner"
			If nr.nCode=200 Then
				g_store.conf.Put("banner",ParseHelper.parseBanner(str))
				nNetReq=nNetReq-1
			End If
		Case "kemu"
			If nr.nCode=200 Then
				g_store.conf.Put(comm.S_SUBJECTS,ParseHelper.parseKemu(str))
				nNetReq=nNetReq-1
			End If
		Case "mstj"
			If nr.nCode=200 Then
				Dim jsLst As List=ParseHelper.parseTeacher(str)
				g_store.conf.Put("mstjlst",jsLst)
				nNetReq=nNetReq-1
			Else
				Log("获取名师推荐出错:"&nr.sData)
				CallSubDelayed2(Main,"onErr",nr.sData)
			End If
		Case "unrate"
			If nr.nCode=200 Then
				Dim parser As JSONParser 
				parser.Initialize(str) 
				Dim root As Map = parser.NextObject 
				Dim data As List = root.Get("data") 
				For Each coldata As Map In data 
				    Dim c As String = coldata.Get("c") 
					nUnrateOrd=c
				Next 
				nNetReq=nNetReq-1
			Else
				comm.L("获取未评价订单数量错误:"&nr.sData)
				g_store.put("islogin",False)
			End If
		Case "getcitys"
			Dim cs As String=g_store.getDef("cityjs","")
			If cs.Length<1 Then nNetReq=nNetReq-1
			g_store.put("cityjs",str)
		End Select
	Else
		comm.TL("name:"&thj.JobName&" Err:"&thj.ErrorMessage)
		If nNetReq>0 Then
			'初始化未完成
			CallSubDelayed2(Main,"onErr",thj.ErrorMessage)
			StopService(Me)
		End If
	End If
'	If nNetReq=0 Then
'		'网络数据初始化完毕
'
'	End If
	thj.Release
End Sub


#End Region
#Region 定位
Sub Service_Destroy
	Try
		g_store.conf.Close	
		aLoc.OnDestory
	Catch
		Log(LastException.Message)
	End Try
	
End Sub
Sub initloc
	aLoc.Init("aloc")
	aloc_option.init
	aloc_option.Interval=3000
	aloc_option.GPSFirst=False
	aloc_option.MockEnable=True
	aloc_option.LocationMode=aloc_option.Battery_Saving
	aloc_option.OnceLocation=True
	aLoc.LocationOption=aloc_option
	aLoc.StartLocation
End Sub
Sub aloc_onLocationChanged(loc As AMapLocation)
'	Dim str As String="addr:"&loc.Address&" City:"&loc.City&" Road:"&loc.Road&" Country:"&loc.Country&" District:"&loc.District&" Latitude:"&loc.Latitude&" Longitude:"&loc.Longitude&" LocationDetail:"&loc.LocationDetail _
'	&" PoiName:"&loc.PoiName&" Province:"&loc.Province&" Street:"&loc.Street&CRLF&CRLF&"Str:"&loc.toStr
'	Log(loc.toStr)
'	'out puts:
'	'{"country":"中国","province":"新疆维吾尔自治区","city":"乌鲁木齐市","cityCode":"0991","district":"新市区","adCode":"650104","address":"新疆维吾尔自治区乌鲁木齐市新市区鲤鱼山路靠近领世广场","road":"鲤鱼山路","street":"鲤鱼山路","number":"298号","poiName":"领世广场","errorCode":0,"errorInfo":"success","locationDetail":"-5","altitude":0,"bearing":0,"speed":0,"satellites":0,"aoiName":"","desc":"新疆维吾尔自治区 乌鲁木齐市 新市区 鲤鱼山路 靠近领世广场","time":1458556366701,"locationType":2,"accuracy":91,"latitude":43.884021,"longitude":87.598392,"provider":"lbs"}
'	edtresult.Text="位置:"&str&CRLF&edtresult.Text
	LocCity=loc
	aLoc.StopLocation
End Sub
#End Region 